﻿// ===============================================================================
// XCI.WinForm.Platform 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using System.Collections.Generic;
using System.Drawing;

namespace XCI.Helper
{
    public static class DrawHelper
    {
        public static SizeF GetSingleSize(Graphics g, Font font)
        {
            return g.MeasureString("微", font);
        }

        /// <summary>
        /// 绘制文本到指定容器(水平垂直居中)
        /// </summary>
        /// <param name="g">绘图对象</param>
        /// <param name="s">文本</param>
        /// <param name="font">字体</param>
        /// <param name="brush">画刷</param>
        /// <param name="parentRectangle">父容器</param>
        /// <param name="xOffset">x坐标偏移量</param>
        /// <param name="yOffset">y坐标偏移量</param>
        public static void DrawStringAsCenter(Graphics g, string s, Font font, Brush brush,
                                              RectangleF parentRectangle, int xOffset = 0, int yOffset = 0)
        {
            var fontSize = g.MeasureString(s, font);
            var x = parentRectangle.X + (parentRectangle.Width - fontSize.Width) / 2 + xOffset;
            var y = parentRectangle.Y + (parentRectangle.Height - fontSize.Height) / 2 + yOffset;
            g.DrawString(s, font, brush, x, y);
        }

        /// <summary>
        /// 绘制文本到指定容器(水平居中)
        /// </summary>
        /// <param name="g">绘图对象</param>
        /// <param name="s">文本</param>
        /// <param name="font">字体</param>
        /// <param name="brush">画刷</param>
        /// <param name="parentRectangle">父容器</param>
        /// <param name="y">y坐标</param>
        public static void DrawStringAsXCenter(Graphics g, string s, Font font, Brush brush, RectangleF parentRectangle, float y)
        {
            var fontSize = g.MeasureString(s, font);
            var x = parentRectangle.X + (parentRectangle.Width - fontSize.Width) / 2;
            g.DrawString(s, font, brush, x, y);
        }

        /// <summary>
        /// 绘制文本到指定容器(垂直居中)
        /// </summary>
        /// <param name="g">绘图对象</param>
        /// <param name="s">文本</param>
        /// <param name="font">字体</param>
        /// <param name="brush">画刷</param>
        /// <param name="parentRectangle">父容器</param>
        /// <param name="x">x坐标</param>
        public static void DrawStringAsYCenter(Graphics g, string s, Font font, Brush brush, RectangleF parentRectangle, float x)
        {
            var fontSize = g.MeasureString(s, font);
            var y = parentRectangle.Y + (parentRectangle.Height - fontSize.Height) / 2;
            g.DrawString(s, font, brush, x, y);
        }

        /// <summary>
        /// 垂直绘制文本
        /// </summary>
        /// <param name="g">绘图对象</param>
        /// <param name="s">文本</param>
        /// <param name="font">字体</param>
        /// <param name="brush">画刷</param>
        /// <param name="x">x坐标</param>
        /// <param name="y">y坐标</param>
        /// <param name="direction">绘制方向(0:从上到下;1:从下到上)</param>
        /// <param name="max">绘制的最大字符数</param>
        /// <param name="maxStr">超出部分文本</param>
        public static void DrawVerticalString(Graphics g, string s, Font font, Brush brush, float x, float y, int direction, int max, string maxStr = "...")
        {
            var _x = x;
            var _y = y;
            int index = 0;
            var charArray = s.ToCharArray();
            var list = new List<string>();
            while (index < max)
            {
                if (index >= charArray.Length)
                {
                    break;
                }
                list.Add(charArray[index].ToString());
                index++;
            }
            if (s.Length > max)
            {
                list.Add(maxStr);
            }
            if (direction == 1)
            {
                list.Reverse();
            }
            for (int i = 0; i < list.Count; i++)
            {
                var cs = list[i];
                var fontSize = g.MeasureString(cs, font);
                if (direction == 1 && i == 0)
                {
                    _y -= fontSize.Height;
                }
                if (i > 0)
                {
                    if (direction == 0)
                    {
                        _y += fontSize.Height * 0.7f;
                    }
                    else
                    {
                        _y -= fontSize.Height * 0.7f;
                    }
                }
                g.DrawString(cs, font, brush, _x, _y);
            }
        }

        /// <summary>
        /// 垂直绘制文本
        /// </summary>
        /// <param name="g">绘图对象</param>
        /// <param name="s">文本</param>
        /// <param name="font">字体</param>
        /// <param name="brush">画刷</param>
        /// <param name="parentRectangle">父容器</param>
        /// <param name="direction">绘制方向(0:从上到下;1:从下到上)</param>
        /// <param name="maxStr">超出部分文本</param>
        public static void DrawVerticalString(Graphics g, string s, Font font,
                                              Brush brush, RectangleF parentRectangle,
                                              int direction, int max = 0, string maxStr = "...")
        {
            var size = GetSingleSize(g, font);
            var _max = Convert.ToInt32(Math.Floor(parentRectangle.Height / (size.Height * 0.8f)));
            if (s.Length > _max)
            {
                _max = max;
            }
            var x = parentRectangle.X + (parentRectangle.Width - size.Width) / 2;
            var y = 0f;

            if (direction == 0)
            {
                y = parentRectangle.Y;
            }
            else
            {
                y = parentRectangle.Y + parentRectangle.Height;
            }

            DrawVerticalString(g, s, font, brush, x, y, direction, _max, maxStr);
        }

        /// <summary>
        /// 获取填充的矩形
        /// </summary>
        /// <param name="rectangle">原始矩形</param>
        /// <param name="borderWidth">边框宽度</param>
        public static Rectangle GetFillRectangle(Rectangle rectangle, int borderWidth)
        {
            return new Rectangle(rectangle.X + borderWidth, rectangle.Y + borderWidth,
                                 rectangle.Width - borderWidth, rectangle.Height - borderWidth);
        }
    }
}